package rpcsupport

import (
	"net/rpc"
	"net"
	"net/rpc/jsonrpc"
	"log"
)

func ServerRpc(host string,service interface{}) error {
	// 注册
	rpc.Register(service)
	// 监听
	listener, e := net.Listen("tcp", host)
	if e != nil {
		return e
	}
	// 等待链接
	for {
		conn, err := listener.Accept()
		if err != nil {
			log.Printf("accept error %v",err)
			continue
		}
		// 开goroutine处理
		go jsonrpc.ServeConn(conn)
	}
	return nil
}

func NewClient(host string )(*rpc.Client,error){
	conn, err := net.Dial("tcp", host)
	if err != nil {
		return nil,err
	}
	return  jsonrpc.NewClient(conn),nil
}
